﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Event.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.Views
{
    public class CheckClientStatusThread
    {
        private bool _isRunning = false;

        private Thread _workThread;

        private static object s_lockObj = new object();

        public static CheckClientStatusThread s_instance;
        private static string Message;
        private ManualResetEvent _manualResetEvent;
        private long _latestTicks = 0;

        private CheckClientStatusThread()
        {
            _manualResetEvent = new ManualResetEvent(false);
        }

        public static void Start(string message)
        {
            Message = message;
            if (s_instance == null)
            {
                lock (s_lockObj)
                {
                    s_instance = new CheckClientStatusThread();
                    
                    Coolzon.Event.NCL.NCLEventBus.Register(s_instance);

                    s_instance._workThread = new Thread(Running);
                    s_instance._workThread.Name = "CheckClientStatusThread";
                    s_instance._isRunning = true;
                    s_instance._workThread.Start();
                }
            }
        }
        public static void Stop()
        {
            lock (s_lockObj)
            {
                if (s_instance != null && s_instance._isRunning)
                {
                    s_instance._isRunning = false;
                    s_instance._manualResetEvent.WaitOne();
                }
            }
        }

        private static void Running()
        {
            UIEventBus.PublishAsync(new UIDisableInteractiveEvent(Message));
            while (s_instance._isRunning)
            {
                if (DateTime.Now.Ticks - s_instance._latestTicks > TimeSpan.FromSeconds(1).Ticks)
                {
                    UIEventBus.PublishAsync(new UIDisableInteractiveEvent(Message));
                    Coolzon.Event.NCL.NCLBeginCheckClientStatusEvent @event = new Coolzon.Event.NCL.NCLBeginCheckClientStatusEvent(Service.Contract.ClientStatus.Normal);
                    Coolzon.Event.NCL.NCLEventBus.Publish(@event);
                    s_instance._latestTicks = DateTime.Now.Ticks;
                    UIEventBus.PublishAsync(new UIEnableInteractiveEvent());
                }
                Thread.Sleep(100);
            }
            s_instance._manualResetEvent.Set();
            s_instance._manualResetEvent = null;
            s_instance = null;
        }

        [Subscribe]
        public void OnReceiveNCLEndCheckClientStatusEvent(Coolzon.Event.NCL.NCLEndCheckClientStatusEvent @event)
        {
            if (@event.IsPass)
            {
                lock (s_lockObj)
                {
                    if (s_instance != null && s_instance._isRunning)
                    {
                        Coolzon.Event.NCL.NCLEventBus.Unregister(s_instance);

                        UIEventBus.PublishAsync(new UIEnableInteractiveEvent());
                        s_instance._isRunning = false;
                    }
                }
            }
        }
    }
}
